// questions.js - 题目及题目来源管理服务模块
import api from './api'

export default {
  // ====== 题目管理方法 ======
  
  /** 
   * 语法: async getQuestions(params)
   * 作用: 获取题目列表
   */
  async getQuestions(params) {
    try {
      const response = await api.get('/questions', { params })
      return response.data
    } catch (error) {
      throw new Error(`获取题目列表失败: ${error.response?.data?.message || error.message}`)
    }
  },

  /** 
   * 语法: async getQuestion(id)
   * 作用: 获取单个题目详情
   */
  async getQuestion(id) {
    try {
      const response = await api.get(`/questions/${id}`)
      return response.data
    } catch (error) {
      throw new Error(`获取题目详情失败: ${error.response?.data?.message || error.message}`)
    }
  },

  /** 
   * 语法: async createQuestion(data)
   * 作用: 创建新题目
   */
  async createQuestion(data) {
     try {
      console.log('333333')
       const response = await api.createQuestion(data)
       return response.data
     } catch (error) {
       throw new Error(`${error.response?.data?.message || error.message}`) //传给组件层
     }
  },
  /** 
   * 语法: async updateQuestion(id, data)
   * 作用: 更新题目信息
   */
  async updateQuestion(id, data) {
    try {
      const response = await api.put(`/questions/${id}`, data)
      return response.data
    } catch (error) {
      throw new Error(`更新题目失败: ${error.response?.data?.message || error.message}`)
    }
  },

  /** 
   * 语法: async deleteQuestion(id)
   * 作用: 删除题目
   */
  async deleteQuestion(id) {
    try {
      const response = await api.delete(`/questions/${id}`)
      return response.data
    } catch (error) {
      throw new Error(`删除题目失败: ${error.response?.data?.message || error.message}`)
    }
  }
}